package com.cold.demo.template.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * mapper SQL执行的耗时信息日志记录
 */
@Aspect
@Component
public class MapperTimeConsumeAspect {

    private Logger log = LoggerFactory.getLogger(MapperTimeConsumeAspect.class.getSimpleName());


    @Pointcut("execution(* com.cold.demo.template.mapper.*.*(..))")
    private void pointCutMethod() {
    }

    /**
     * 声明环绕通知
     *
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Signature signature  = pjp.getSignature();
        String    className  = signature.getDeclaringTypeName();
        String    methodName = signature.getName();
        log.info("==>执行方法：{}#{}(..)", className, methodName);
        long   start = System.currentTimeMillis();
        Object obj   = pjp.proceed();
        long   end   = System.currentTimeMillis();
        log.info("<==消耗时间：{}ms", (end - start));
        return obj;
    }
}